from django.db import models
from datetime import datetime
from DjangoUeditor.models import UEditorField
from ckeditor.fields import RichTextField


# Create your models here.


class GoodsCategory(models.Model):
    """
    商品类别
    """
    CategoryType = (("1", "一级类目"), ("2", "二级类目"), ("3", "三级类目"))
    name = models.CharField(null=True, blank=True, max_length=30, help_text='类别名', verbose_name='类别名')
    code = models.CharField(null=True, blank=True, max_length=30, help_text='类名编号', verbose_name='类名编号')
    desc = models.TextField(default="", null=True, blank=True, help_text='类别描述', verbose_name='类别描述')
    category_type = models.CharField(max_length=10, null=True, blank=True, choices=CategoryType, help_text='类目级别'
                                     , verbose_name='类目级别')
    parent_category = models.ForeignKey('self', null=True, blank=True, help_text='父类目级', db_constraint=False,
                                        related_name="sub_cat", verbose_name='父类目级')
    is_tab = models.BooleanField(default=False, help_text='是否导航', verbose_name='是否导航')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间', verbose_name='添加时间')

    class Meta:
        verbose_name = "商品类别"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsCategoryBrand(models.Model):
    """
    品牌名
    """
    category = models.ForeignKey(GoodsCategory, null=True, blank=True, help_text='商品类目', related_name='brands')
    name = models.CharField(null=True, blank=True, max_length=30, help_text='品牌名')
    desc = models.TextField(null=True, blank=True, help_text='品牌描述')
    image = models.ImageField(max_length=200, upload_to="brand/", help_text='品牌图片')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = "品牌"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    """
    商品
    """
    category = models.ForeignKey(GoodsCategory, null=True, blank=True, db_constraint=False, related_name='goods_cat')
    goods_sn = models.CharField(max_length=50, null=True, blank=True, help_text='唯一货号', verbose_name='唯一货号')
    name = models.CharField(max_length=300, null=True, blank=True, help_text='商品名', verbose_name='商品名')
    click_num = models.IntegerField(default=0, help_text='点击数', verbose_name='点击数')
    sold_num = models.IntegerField(default=0, help_text='销售量', verbose_name='销售量')
    fav_num = models.IntegerField(default=0, help_text='收藏数', verbose_name='收藏数')
    storage_num = models.IntegerField(default=0, help_text='库存量', verbose_name='库存量')
    market_price = models.FloatField(default=0, help_text='市场价格', verbose_name='市场价格')
    shop_price = models.FloatField(default=0, help_text='本店价格', verbose_name='价格')
    goods_brief = models.TextField(max_length=500, null=True, blank=True, help_text='商品简介', verbose_name='商品简介')
    goods_desc = UEditorField(imagePath="goods/images/", width=1000, height=300, filePath="goods/file/",
                              help_text='内容', null=True, blank=True, verbose_name='内容')
    ship_free = models.BooleanField(default=False, help_text='是否包邮', verbose_name='是否包邮')
    goods_front_image = models.ImageField(null=True, blank=True, upload_to="goods/images", help_text='封面图'
                                          , verbose_name='封面图')
    is_new = models.BooleanField(default=True, verbose_name='是否新品', help_text='是否新品')
    is_hot = models.BooleanField(default=False, verbose_name='是否热销', help_text='是否热销')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间', verbose_name='添加时间')

    class Meta:
        verbose_name = "商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsImage(models.Model):
    """
    商品轮播图
    """
    goods = models.ForeignKey(Goods, null=True, blank=True, db_constraint=False, help_text='商品ID',
                              related_name='images')
    image = models.ImageField(upload_to="goods/images_index", null=True, blank=True)
    index = models.IntegerField(default=0, help_text='轮播顺序')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = "商品轮播图"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Banner(models.Model):
    """
    轮播商品
    """
    goods = models.ForeignKey(Goods, db_constraint=False, null=True, blank=True, help_text='商品ID')
    image = models.ImageField(upload_to="banner/", null=True, blank=True, help_text='轮播图片')
    index = models.IntegerField(default=0, null=True, blank=True, help_text='轮播顺序')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = "轮播商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class HotSearchWords(models.Model):
    """
    热搜词
    """
    keywords = models.CharField(default="", max_length=20, verbose_name="热搜词")
    index = models.IntegerField(default=0, verbose_name="排序")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = '热搜词'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.keywords


class IndexAD(models.Model):
    category = models.ForeignKey(GoodsCategory, db_constraint=False, related_name='category', help_text='商品类目',
                                 verbose_name='商品类目')
    goods = models.ForeignKey(Goods, related_name='goods', db_constraint=False, help_text='商品', verbose_name='商品')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间', verbose_name='添加时间')

    class Meta:
        verbose_name = '首页商品类别广告'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name
